1566C - MAX-MEX Cut - CodeForces Solution


bitmasks constructive algorithms dp greedy *1000

Please click on ads to support us..

Python Code:

def max_mex(arr):
	ptr=0
	tot=0
	while ptr<len(arr[0]):
		if arr[0][ptr]!=arr[1][ptr]:
			tot+=2
			ptr+=1
		else:
			if arr[0][ptr]=="1" and ptr+1<len(arr[0]):
				if arr[0][ptr+1]!=arr[1][ptr+1]:
					ptr+=1
				elif arr[0][ptr+1]=="1":
					ptr+=1
				else:
					tot+=2
					ptr+=2
			elif arr[0][ptr]=="0" and ptr+1<len(arr[0]):
				if arr[0][ptr+1]!=arr[1][ptr+1]:
					tot+=1
					ptr+=1
				elif arr[0][ptr+1]=="1":
					ptr+=2
					tot+=2
				else:
					tot+=1
					ptr+=1
			else:
				tot+=1 if arr[0][ptr]=="0" else 0
				ptr+=1
	return tot
t=input()
for _ in range(int(t)):
	length=input()
	row_1=input()
	row_2=input()
	arr=[row_1,row_2]
	print(max_mex(arr))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define lowbit(x) (x & (-x))
 
ll fastPow(ll a,ll n,ll mod){ll ans=1; a%=mod; while(n){ if(n&1) ans=(ans*a)%mod; a=(a*a)%mod; n>>=1;} return ans;}
int gcd(int a,int b){if(b) while((a%b)&&(b%=a));return a+b;}
int lcm(int a,int b){return a*b/gcd(a,b);}
int read(){int x=0,y=1;char s=getchar();while(s<'0'||s>'9'){if(s=='-')y=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}return x*y;}

const int N=100005;
int a[N],b[N],c[N],n;

void solve(){
	cin>>n;
	int cnt=0,tmp=-1;
	string s;
	cin>>s;
	for(int i=1;i<=n;i++){
        a[i]=s[i-1]-'0';
	}
	cin>>s;
	for(int i=1;i<=n;i++){
        b[i]=s[i-1]-'0';
	}
	for(int i=1;i<=n;i++){
		c[i]=a[i]+b[i];
		if(c[i]==1){
			cnt=cnt+2;
			if(tmp==0)
			    cnt++;
			tmp=-1;
		}
		else if(tmp==-1)
		    tmp=c[i];
		else{
			if(tmp!=c[i]){
				cnt+=2;
				tmp=-1;
			}
			else{
				if(tmp==0)
				    cnt++;
				tmp=c[i];
			}
		}
	}
	if(tmp==0)
	    cnt++;
	cout<<cnt<<"\n"; 
	return;
}

int main(){
    //ios::sync_with_stdio(0);
	//cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	    solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces